#include "XML_Loader.h"
#include "App.h"
#include "Track.h"
#include "Jet.h"

extern DebugTracer _DEBUGTRACER;
XML_Loader::XML_Loader()
{
    //ctor
}

XML_Loader::~XML_Loader()
{
    //dtor
}

void XML_Loader::LoadFromXmlFile(Track* track,const std::string& fileName)
{
	
    TiXmlDocument doc(fileName.c_str());
    //doc.LoadFile();
	doc.Parse(GetFileContent(fileName), 0, TIXML_ENCODING_UTF8);
	
    TiXmlHandle hDoc(&doc);
    TiXmlElement *track_elem = hDoc.FirstChild("Track").Element();
    track->SetName(track_elem->Attribute("name"));

    long counter = 0;
    while(hDoc.FirstChild("Track").Child("node",counter).ToElement() != NULL)
    {
        TiXmlElement* node_elem = hDoc.FirstChild("Track").Child("node",counter).ToElement();
        TrackNode node;
        node_elem->QueryFloatAttribute("size",&node.Size);
        node.Texture = node_elem->Attribute("material");

        TiXmlElement* position_elem = hDoc.FirstChild("Track").Child("node",counter).Child("position",0).ToElement();
        position_elem->QueryFloatAttribute("x",&node.Position.x);
        position_elem->QueryFloatAttribute("y",&node.Position.y);
        position_elem->QueryFloatAttribute("z",&node.Position.z);

        TiXmlElement* normal_elem = hDoc.FirstChild("Track").Child("node",counter).Child("normal",0).ToElement();
        normal_elem->QueryFloatAttribute("x",&node.Normal.x);
        normal_elem->QueryFloatAttribute("y",&node.Normal.y);
        normal_elem->QueryFloatAttribute("z",&node.Normal.z);

        track->AddTrackNode(node.Position,node.Normal,node.Texture,node.Size);
        counter++;
    }
}

void XML_Loader::SaveTrack(Track* track, const std::string& fileName)
{
    TiXmlDocument doc;

    TiXmlElement *trackElem = new TiXmlElement("Track");
    trackElem->SetAttribute("name",track->GetName().c_str());
    doc.LinkEndChild(trackElem);

    TiXmlElement *nodeElem = NULL;
    TiXmlElement *posElem = NULL;
    TiXmlElement *normElem = NULL;

    std::vector<TrackNode>::iterator it;
    for(it = track->GetTrackNodeList().begin(); it < track->GetTrackNodeList().end(); it++)
    {
        nodeElem = new TiXmlElement("node");
        nodeElem->SetAttribute("size",(*it).Size);
        nodeElem->SetAttribute("material",(*it).Texture.c_str());

        posElem = new TiXmlElement("position");
        posElem->SetAttribute("x",(*it).Position.x);
        posElem->SetAttribute("y",(*it).Position.y);
        posElem->SetAttribute("z",(*it).Position.z);
        nodeElem->LinkEndChild(posElem);

        normElem = new TiXmlElement("normal");
        normElem->SetAttribute("x",(*it).Normal.x);
        normElem->SetAttribute("y",(*it).Normal.y);
        normElem->SetAttribute("z",(*it).Normal.z);
        nodeElem->LinkEndChild(normElem);

        trackElem->LinkEndChild(nodeElem);

    }


    doc.SaveFile(GetFileTruePath(fileName));

}

void XML_Loader::LoadGameSettings(App* app, const std::string& filename)
{
}

void XML_Loader::SaveGameSettings(App* app, const std::string& filename)
{
}

const char* XML_Loader::GetFileTruePath(const std::string& resFile){
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *documentsDirectory = [paths objectAtIndex:0]; 
	NSString *configPath = [documentsDirectory stringByAppendingPathComponent:@"config/"];
	NSString *xmlFile = [configPath stringByAppendingPathComponent:[[NSString alloc] initWithCString:resFile.c_str()]];
	
	return [xmlFile cStringUsingEncoding:[NSString defaultCStringEncoding]];
}

const char* XML_Loader::GetFileContent(const std::string& resFile)
{
	NSString *content = [[NSString alloc] initWithContentsOfFile:[[NSString alloc] initWithCString:GetFileTruePath(resFile)]];
	const char* cContent = [content cStringUsingEncoding:[NSString defaultCStringEncoding]];
	
	std::cout << "File Content : " << std::endl << cContent << std::endl;
	return cContent;
}

bool XML_Loader::LoadJetFromXmlFile(Jet* jet, std::string const& fileName, std::string const& jet_name)
{
    TiXmlDocument doc(fileName.c_str());
	
	doc.Parse(GetFileContent(fileName));

	TiXmlNode * pNode = doc.FirstChild();
	
	TiXmlElement* element = pNode->ToElement();
	std::string name = element->Attribute("name");
	if(name == jet_name)
	{
		jet->SetAuthor(element->Attribute("author"));
		std::cout << "Author:" << jet->GetAuthor() << std::endl;
		jet->SetMeshName(pNode->FirstChildElement("mesh")->ToElement()->Attribute("file"));
		std::cout << "MeshName:" << jet->GetMeshName() << std::endl;
		float braking;
		float acceleration;
		float top_speed;
		float handling;
		float shield;
		TiXmlElement* skill = pNode->FirstChildElement("skill")->ToElement();
		std::cout << "skill:" << skill << std::endl;
		skill->QueryFloatAttribute("acceleration", &acceleration);
		std::cout << "acceleration:" << acceleration << std::endl;
		skill->QueryFloatAttribute("top_speed",&top_speed);
		std::cout << "top_speed:" << top_speed << std::endl;
		skill->QueryFloatAttribute("handling",&handling);
		std::cout << "handling:" << handling << std::endl;
		skill->QueryFloatAttribute("braking",&braking);
		std::cout << "braking:" << braking << std::endl;
		skill->QueryFloatAttribute("shield",&shield);
		std::cout << "shield:" << shield << std::endl;
		
		if(jet->AdjustSkills(acceleration,top_speed,handling,shield,braking))
			return true;
		else
			return false;
	}

    return false;
}
